package cn.zshprint.serein.service.impl;

import cn.zshprint.serein.service.PermissionRoleRelationService;
import cn.zshprint.serein.vo.RoleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zshprint.serein.entity.Role;
import cn.zshprint.serein.service.RoleService;
import cn.zshprint.serein.mapper.RoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    @Resource
    private PermissionRoleRelationService permissionRoleRelationService;

    @Override
    public Role selectOne(Integer id) {
        return getById(id);
    }

    @Override
    public List<Role> selectAll() {
        return list();
    }

    @Override
    public Integer save(String name, String description) {
        Role role = new Role()
                .setName(name)
                .setDescription(description);
        role.setCreateTime(LocalDateTime.now());
        save(role);
        return role.getId();
    }

    @Override
    public Integer saveByJson(RoleVO roleVO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVO, role);
        role.setCreateTime(LocalDateTime.now());
        save(role);
        return role.getId();
    }

    @Override
    public Role update(Role role) {
        updateById(role);
        return role;
    }

    @Override
    public Integer delete(Integer id) {
        update(new UpdateWrapper<Role>()
                .eq("id", id)
                .set("status", 0));
        return id;
    }
}




